/**
 * 
 */
package com.swg.cocobox.filetransfer.example;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;

import com.swg.cocobox.filetransfer.FileClient;
import com.swg.cocobox.filetransfer.command.CommandContext;
import com.swg.cocobox.filetransfer.command.ReplyCommand;
import com.swg.cocobox.filetransfer.command.CommandContext.CommandEncoding;
import com.swg.cocobox.filetransfer.data.DataReceiver;
import com.swg.cocobox.filetransfer.data.FileResponseHandler;
import com.swg.cocobox.filetransfer.data.TransferResponse;

/**
 * @author satriaprayoga
 *
 */
public class SimpleFileClient extends FileClient{
	
	private final Logger logger=Logger.getLogger(getClass());
	
	private DataReceiver receiver;
	private ExecutorService service=Executors.newCachedThreadPool();

	public SimpleFileClient(InetAddress address, int port) throws IOException {
		super(address, port);
		setClientCommandFactory(CommandContext.getClientCommandFactory(CommandEncoding.ASCII));
		setReplyCommandFactory(CommandContext.getReplyCommandFactory(CommandEncoding.ASCII));
		receiver=new DataReceiver(address, CommandContext.DEFAULT_DATAPORT);
	}
	
	@Override
	public synchronized void startClient() {
		Thread clientThread=new Thread(this);
		clientThread.start();
		service.execute(receiver);
	}

	@Override
	protected void handleReply(SocketChannel channel,ReplyCommand command) throws IOException {
		int code=command.getCode();
		switch (code) {
		case CommandContext.FILE_STATUS_OK:
			handleFileOk(channel,command);
			break;
		case CommandContext.COMMAND_OK:
			handleCommandOk(command);
			break;
		default:
			handle(command);
			break;
		}
	
	}
	
	private void handle(ReplyCommand command)throws IOException{
		logger.info("code: "+command.getCode()+" message: "+command.getMessage());
	}
	
	private void handleCommandOk(ReplyCommand command)throws IOException{
		logger.info("code: "+command.getCode()+" message: "+command.getMessage());
		lprt(command.getMessage(), new FileResponseHandler());
	}
	
	private void handleFileOk(SocketChannel channel,ReplyCommand command)throws IOException{
		logger.info("code: "+command.getCode()+" message: "+command.getMessage());
		String [] fileInfo=command.getMessage().split("\\s");
		String fileName=fileInfo[0];
		int size=Integer.parseInt(fileInfo[1]);
		File file=new File(System.getProperty("user.home")+File.separator+fileName);
		TransferResponse response=new TransferResponse(file);
		response.setFileSize(size);
		receiver.processResponse(response);
		
	}
	
	public static void main(String[] args)throws IOException{
		FileClient client=new SimpleFileClient(InetAddress.getByName("127.0.0.1"), 1234);
		client.startClient();
		client.retrive("netty.pdf", new FileResponseHandler());
		client.retrive("ROX.pdf", new FileResponseHandler());
	}

}
